<template>
	<view clas="notice">
		<view class="notice-item" v-for="item in items" :key="item.id">
			<text class="time">{{ item.created_at | time }}</text>
			<view class="content" @tap="navTo(`/pages/notice/view?id=${item.id}`)" >
				<text class="title">{{ item.title }}</text>
				<view class="img-wrapper" v-if="item.cover">
					<rf-image class="pic" :preview="false" :mode="'aspectFit'" :src="item.cover" ></rf-image>
				</view>
				
				<text class="introduce" v-if="item.synopsis">
					{{ item.synopsis }}
				</text>
				
				<view class="bot b-t">
					<text>查看详情</text>
					<i class="more-icon iconfont iconyou"></i>
				</view>
			</view>
		</view>

		<rf-load-more class="load-more" :status="loadingType" v-if="items.length > 0" ></rf-load-more>
		<rf-empty info="暂无公告" v-if="items.length === 0 && !loading" ></rf-empty>

		<!--加载动画-->
		<rfLoading isFullScreen :active="loading"></rfLoading>
	</view>
</template>

<script>
import moment from '@/common/moment';
import rfLoadMore from '@/components/rf-load-more/rf-load-more.vue';
import { noticeList } from '@/api/core';

export default {
	components: { rfLoadMore },
	data() {
		return {
			loadingType: 'more',
			loading: true,
			items: [],
			page: 1
		};
	},
	filters: {
		// 时间格式化
		time(val) {
			return moment(val * 1000).format('YYYY-MM-DD HH:mm');
		}
	},
	// 下拉刷新
	onPullDownRefresh() {
		this.page = 1;
		this.announceList.length = 0;
		this.getNoticeList('refresh');
	},
	// 加载更多
	onReachBottom() {
  if (this.loadingType === 'nomore') return;
		this.page++;
		this.getNoticeList();
	},
	onLoad() {
		this.initData();
	},
	methods: {
		// 数据初始化
		initData() {
			this.getNoticeList();
		},
		// 获取通知列表
		async getNoticeList(type) {
			await this.$http.get(`${noticeList}`, { page: this.page }).then(r => {
				this.loading = false;
				
				if (type === 'refresh') {
					uni.stopPullDownRefresh();
				}
				
				this.loadingType = r.data.length < 10 ? 'nomore' : 'more';
				this.items = [...this.items, ...r.data];
			}).catch(() => {
				if (type === 'refresh') {
					uni.stopPullDownRefresh();
				}

				this.loading = false;
			});
		},
		navTo(route) {
			this.$mRouter.push({ route });
		}
	}
};
</script>

<style lang="scss">
page {
	background-color: $page-color-base;
	padding-bottom: 30rpx;
}

.notice-item {
	display: flex;
	flex-direction: column;
	align-items: center;
}

.time {
	display: flex;
	align-items: center;
	justify-content: center;
	height: 80rpx;
	padding-top: 10rpx;
	font-size: 26rpx;
	color: #7d7d7d;
}

.content {
	width: 710rpx;
	padding: 0 24rpx;
	background-color: #fff;
	border-radius: 4rpx;
}

.title {
	display: flex;
	align-items: center;
	height: 90rpx;
	font-size: 32rpx;
	color: #303133;
}

.img-wrapper {
	width: 100%;
	height: 260rpx;
	position: relative;
}

.pic {
	display: block;
	width: 100%;
	height: 100%;
	border-radius: 6rpx;
}

.cover {
	display: flex;
	justify-content: center;
	align-items: center;
	position: absolute;
	left: 0;
	top: 0;
	width: 100%;
	height: 100%;
	background-color: rgba(0, 0, 0, 0.5);
	font-size: 36rpx;
	color: #fff;
}

.introduce {
	display: inline-block;
	padding: 16rpx 0;
	font-size: 28rpx;
	color: #606266;
	line-height: 38rpx;
}

.bot {
	display: flex;
	align-items: center;
	justify-content: space-between;
	height: 80rpx;
	font-size: 24rpx;
	color: #707070;
	position: relative;
}

.more-icon {
	font-size: 32rpx;
}
</style>
